package cn.gok.controller;


import cn.gok.entity.User;
import cn.gok.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/user/")
public class UserController {

    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public JSONObject login(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("userName",json.getString("userName"));
        map.put("password",json.getString("password"));
        JSONObject result=new JSONObject();
        if (userService.sel(map).size()!=0){
            result.put("code",1);
            result.put("data",userService.sel(map));
        }else {
            result.put("code",0);
        }
        return result;
    }
    //注册
    @RequestMapping("/register")
    @ResponseBody
    public JSONObject registered(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        map.put("userName",json.getString("userName"));
        map.put("password",json.getString("password"));
        map.put("phone",json.getString("phone"));
        map.put("sex",json.getString("sex"));
        JSONObject result=new JSONObject();
        result.put("code",userService.add(map));
        return result;
    }
    //找回密码
    @RequestMapping("/back")
    @ResponseBody
    public JSONObject back(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("userName",json.getString("userName"));
        map.put("password",json.getString("password"));
        map.put("phone",json.getString("phone"));
        JSONObject result=new JSONObject();
        result.put("code",userService.updata(map));
        return result;
    }
    //列表
    @RequestMapping("/show")
    @ResponseBody
    public JSONObject show(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("searchKey",json.getString("searchKey"));
//        map.put("start",json.getInteger("start"));
//        map.put("pagesize",json.getInteger("pagesize"));
        JSONObject result =new JSONObject();
        result.put("data",userService.show(map));
        result.put("total",userService.show(map).size());
        return result;
    }
    //增加
    @RequestMapping("/add")
    @ResponseBody
    public JSONObject add(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        map.put("userName",json.getString("userName"));
        map.put("phone",json.getString("phone"));
        map.put("password",json.getString("password"));
        JSONObject result =new JSONObject();
        result.put("code",userService.add(map));
        return result;
    }
    //修改
    @RequestMapping("/re")
    @ResponseBody
    public JSONObject re(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        map.put("userName",json.getString("userName"));
        map.put("phone",json.getString("phone"));
        map.put("password",json.getString("password"));
        map.put("sex",json.getString("sex"));
        JSONObject result =new JSONObject();
        if (userService.re(map)!=0){
            result.put("code",userService.re(map));
            result.put("msg","修改成功");
        }else {
            result.put("msg","修改失败");
        }
        return result;
    }
    //删除
    @RequestMapping("/del")
    @ResponseBody
    public JSONObject del(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("userName",json.getString("userName"));
        JSONObject result =new JSONObject();
        if (userService.del(map)==1){
            result.put("code",1);
            result.put("msg","删除成功");
        }else {
            result.put("code",0);
            result.put("msg","删除失败");
        }
        return result;
    }
    @RequestMapping("/details")
    @ResponseBody
    public JSONObject details(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("userName",json.getString("userName"));
        JSONObject result =new JSONObject();
        result.put("data",userService.details(map));
        return result;
    }
}
